package com.petalsgroup.petals.web.servlet.animation;

import com.petalsgroup.petals.pojo.AnimationSimpleVO;
import com.petalsgroup.petals.pojo.ResultInfo;
import com.petalsgroup.petals.pojo.SearchMessage;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.service.animation.impl.AnimationServiceImpl;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.web.BaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/animation/*")
public class AnimationServlet extends BaseServlet {
    private final AnimationService animationService=new AnimationServiceImpl();

    public void show_detailed(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("search");
        //从首页缩略图点击传到servlet转发的动漫aid数据
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //调用collectFind查询用户是否收藏
        collectFind(request,response,aid);
        //获取动漫详细页基本信息
        request.setAttribute("animationDetailedVO",animationService.getAnimationDetailsVO(aid));
        //获取动漫详细页的相关推荐信息
        request.setAttribute("animationMore",animationService.getAnimationMore(aid));
        request.getRequestDispatcher("/WEB-INF/animation/animation_detailed.jsp").forward(request,response);
    }

    public void showAllSimple(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        // request.removeAttribute("allAnimationSimple");
        // 目录界面获取所有动漫simpleVO简介
        // request.setAttribute("allAnimationSimple",animationService.getAllAnimationSimpleVO());
        LimitUtil limit = new LimitUtil();
        SearchMessage search = (SearchMessage) request.getSession().getAttribute("search");
        if (search == null) {
            search = new SearchMessage();
        }
        request.getSession().setAttribute("search", search);
        request.setAttribute("allAnimationSimple",animationService.listSearch(search, limit));
        request.setAttribute("limit", limit);
        request.getRequestDispatcher("/WEB-INF/animation/animation_catalogue.jsp").forward(request,response);
    }

    /**
     * 首页初始化
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("search");
        LimitUtil recommendedLimit = new LimitUtil();
        LimitUtil recentlyLimit = new LimitUtil();
        // 推荐
        request.setAttribute("Recommended", animationService.listRecommended(recommendedLimit));
        request.setAttribute("RecommendedLimit", recommendedLimit);
        // 最近更新
        request.setAttribute("Recently", animationService.listRecently(recentlyLimit));
        request.setAttribute("RecentlyLimit", recentlyLimit);

        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * 主页分页跳转
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void indexJump(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        // 获取需要更新的模块
        String model = request.getParameter("model");
        // 获取记录总数
        LimitUtil limit = new LimitUtil(Long.valueOf(request.getParameter("total")),
                Integer.valueOf(request.getParameter("num")),
                Integer.valueOf(request.getParameter("currentPage")));
        List<AnimationSimpleVO> list = animationService.flashIndexContext(model, limit);
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("limit", limit);
        map.put("list", list);
        ResultInfo info = new ResultInfo();
        info.setData(map);
        writeValue(response, info);
    }


    /**
     * 每周放送
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void weekFlash(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String week = request.getParameter("week");
        ResultInfo info = new ResultInfo();
        // 每周放送
        List<AnimationSimpleVO> list = animationService.listFlash(week);
        if (list == null) {
            info.setFlag(false);
            info.setErrorMsg("抱歉," + week + "没有动漫更新!");
        } else {
            info.setFlag(true);
            info.setData(list);
        }
        writeValue(response, info);
    }

    /**
     * 搜索框
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void toSearch(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String parameter = request.getParameter("search");
        SearchMessage search = new SearchMessage();
        search.setAname(parameter);
        request.getSession().setAttribute("search", search);
        response.sendRedirect(request.getContextPath() + "/animation/showAllSimple");
    }

    /**
     * 分类搜索
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void search(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        SearchMessage search = new SearchMessage();
        String parameter = request.getParameter("cid");
        if (parameter != null && !parameter.equals("")) {
            search.setCid(Integer.valueOf(parameter));
        }
        parameter = request.getParameter("aname");
        //System.out.println(parameter);
        if (parameter != null && !parameter.equals("")) {
            search.setAname(parameter);
        }
        parameter = request.getParameter("year");
        if (!parameter.equals("全部")) {
            search.setYear(Integer.valueOf(parameter));
        }
        parameter = request.getParameter("quarter");
        if (!parameter.equals("全部")) {
            search.setQuarter(Integer.valueOf(parameter));
        }
        parameter = request.getParameter("state");
        if (!parameter.equals("全部")) {
            search.setState(parameter);
        }
        parameter = request.getParameter("sort");
        if (!parameter.equals("全部")){
            search.setSort(parameter);
        }
        LimitUtil limit = new LimitUtil();
        request.getSession().setAttribute("search", search);
        List<AnimationSimpleVO> list = animationService.listSearch(search, limit);
        ResultInfo info = new ResultInfo();
        if (list == null || list.size() <= 0) {
            info.setFlag(false);
            info.setErrorMsg("没有找到相应的动漫！");
        } else {
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("limit", limit);
            info.setFlag(true);
            info.setData(map);
        }
        writeValue(response, info);
    }

    /**
     * 分类页面跳转
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void searchJump(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String total = request.getParameter("total");
        String num = request.getParameter("num");
        String currentPage = request.getParameter("currentPage");
        LimitUtil limit = new LimitUtil(Long.valueOf(total), Integer.valueOf(num), Integer.valueOf(currentPage));
        SearchMessage search = (SearchMessage) request.getSession().getAttribute("search");
        List<AnimationSimpleVO> list = animationService.listSearch(search, limit);
        ResultInfo info = new ResultInfo();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("limit", limit);
        info.setFlag(true);
        info.setData(map);
        writeValue(response, info);
    }

    // 添加收藏
    public void collectAdd(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取Session域的user数据
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        //获取收藏按钮点击事件传的aid
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        boolean flag=animationService.addCollectAnimation(aid,user.getUid());
        writeValue(response, new ResultInfo(flag));
    }

    // 移除收藏
    public void collectRemove(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取Session域的user数据
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        //获取收藏按钮点击事件传的aid
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        boolean flag=animationService.removeCollectAnimation(aid,user.getUid());
        writeValue(response,new ResultInfo(flag));
    }

    // 判断是否收藏
    private void isCollect(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取Session域的user数据
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        int aid = Integer.parseInt(request.getParameter("aid"));
        boolean flag=animationService.findCollectAnimation(aid,user.getUid());
        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        writeValue(response, info);
    }

    // 判断是否收藏
    private void collectFind(HttpServletRequest request,HttpServletResponse response,Integer aid){
        //获取Session域的user数据
        UserDO user = (UserDO) request.getSession().getAttribute("user");
        boolean flag=animationService.findCollectAnimation(aid,user.getUid());
        request.setAttribute("isCollect", flag);
    }

    /**
     * 进入排行榜
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void topList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("search");
        request.getRequestDispatcher("/WEB-INF/animation/topList.jsp").forward(request, response);
    }

    /**
     * 更新排行榜
     * @author zqm
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void flashTop(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String time = request.getParameter("time");
        SearchMessage search = new SearchMessage();
        if (!(time == null || time.equals(""))) {
            search.setYear(Integer.valueOf(time));
        }
        search.setSort("评分");
        List<AnimationSimpleVO> list = animationService.listSearch(search);
        ResultInfo info = new ResultInfo();
        if (list == null || list.size() == 0) {
            info.setFlag(false);
            info.setErrorMsg("这一年没有动漫");
        } else {
            info.setFlag(true);
            info.setData(list);
        }
        writeValue(response, info);
    }
}
